﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/19
 * 
 * 功能说明：广告JS处理类
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;


public class AdZone
{
    public static void CreateJS(int adId, int modelID)
    {
        Whir.Model.Whir_Model modelModel = new Whir.Model.Whir_Model();
        Whir.BLL.Whir_Model modelbll = new Whir.BLL.Whir_Model();
        modelModel = modelbll.GetModel(modelID);

        //获取广告表名
        string tableName = modelModel.tableName;
        //获取广告表的主键ID
        string primaryKeyName = ContentManage.GetCustomTablePrimaryKeyName(tableName);
        //获取该广告的信息       
        string adTypeID;
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("*", tableName, primaryKeyName + "=" + adId.ToString(), "");
        adTypeID = dt.Rows[0]["adTypeID"].ToString();

        string adFileContent = GetAdJSContent(adId, adTypeID, dt, modelID);
        string filePath = Whir.Common.Settings.GetString("AdPath");
        FileSystemObject.WriteFile(HttpContext.Current.Server.MapPath(filePath + adId + ".js"), adFileContent);
    }

    private static string GetAdJSContent(int adId, string adTypeID, DataTable dt, int modelId)
    {
        string content = "";

        string zoneID = Whir.SQLServerDAL.DbHelperSQL.GetSingle("SELECT adZoneID FROM Whir_U_Ad WHERE Whir_U_Ad_PID = " + adTypeID).ToString();

        string jsPath = Whir.SQLServerDAL.DbHelperSQL.GetSingle("SELECT JsPath FROM whir_AdZone WHERE AdZoneID = " + zoneID).ToString();

        content = FileSystemObject.ReadFile(HttpContext.Current.Server.MapPath(jsPath));

        content += GetAdJsParam(adId, zoneID, dt, modelId);

        return content;
    }

    private static string GetAdJsParam(int adId, string zoneID, DataTable dt, int modelId)
    {
        StringBuilder sb = new StringBuilder();
        string adTypeID, imageUrl, linkUrl, closeImageUrl;
        adTypeID = dt.Rows[0]["adTypeID"].ToString();
        imageUrl = dt.Rows[0]["imageUrl"].ToString();
        linkUrl = Whir.Common.Settings.GetString("AdminPath") + "Module/Advertisement/AdCount.ashx?action=click&adId=" + adId + "&modelId=" + modelId;
        closeImageUrl = Whir.Common.Settings.GetString("SystemPath") + "UploadFiles/image/close.gif";

        if (imageUrl.StartsWith("~/"))
        {
            imageUrl = imageUrl.TrimStart('~');
            imageUrl = imageUrl.TrimStart('/');
            imageUrl = Whir.Common.Settings.GetString("SystemPath") + imageUrl;
        }

        if (zoneID == "3")//两侧浮动
        {
            sb.Append(string.Format("var ad_{0} = new AdObject();", adId));
            sb.Append(string.Format("this.leftId = \"leftAd_{0}\";", adId));
            sb.Append(string.Format("this.leftImageUrl = \"{0}\";", imageUrl));
            sb.Append(string.Format("this.rightId = \"rightAd_{0}\";", adId));
            sb.Append(string.Format("this.rightImageUrl = \"{0}\";", imageUrl));
            sb.Append(string.Format("this.linkUrl = \"{0}\";", linkUrl));
            sb.Append(string.Format("this.closeImageUrl = \"{0}\";", closeImageUrl));
            sb.Append(string.Format("show(ad_{0});", adId));
        }
        else if (zoneID == "4")//漂浮广告
        {
            sb.Append(string.Format("adObject(\"adImage{0}\",\"{1}\",\"{2}\");start();", adId, imageUrl, linkUrl));
        }
        else
        { 
            
        }
        return sb.ToString();
    }
}